var express = require('express');
var router = express.Router();
let path = require('path');
let fs = require('fs');

/* GET users listing. */
router.get('/login', function (req, res, next) {
  res.render('login')
});

router.post('/login', (req, res, next) => {
  //(1)、获取前端传递过来的参数
  let {
    username,
    password
  } = req.body; // { username: '414', password: '414' }

  //(2)、获取persones.json里面
  let data = JSON.parse(fs.readFileSync(path.join(__dirname, '../persons.json')));

  //(3)、判断账号和密码是否一直，如果一直提示登入成功, 否则账号和密码不一致
  let s = data.find(item => username == item.username && password == item.password);
  if (s) {
    res.render('aa',{zhengque: '登录成功'})
    return;
  }
  next('账号和密码不一致');
})

module.exports = router;